/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Server.ImplementInterface;

import DAL.DBManager;
import Server.DTO.DailyReport_DTO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author 10520_000
 */
public class DailyReportRepository {
    public static int Insert(DailyReport_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "INSERT INTO DAILYREPORT(ReportID, ReportingDate) Values(?,?)";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            pstmt.setString(1, info.getReportID());
            pstmt.setDate(2, (Date) info.getReportingDate());
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static int Update(DailyReport_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "UPDATE DAILYREPORT SET ReportID= ?,ReportingDate= ?, WHERE ReportID= ?";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            pstmt.setString(1, info.getReportID());
            pstmt.setDate(2, (Date) info.getReportingDate());
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static int Delete(DailyReport_DTO info){
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "DELETE DAILYREPORT WHERE ReportID= ?";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            pstmt.setString(1, info.getReportID());
            pstmt.setDate(2, (Date) info.getReportingDate());
            iRet = pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static ArrayList FindAll() {
        ArrayList arr = new ArrayList();

        try {
            String sql = "SELECT * FROM DAILYREPORT ORDER BY CustomerID";
            Connection con = DBManager.getInstance().getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                DailyReport_DTO info = new DailyReport_DTO();
                info.setReportID(rs.getString("ReportID"));
                info.setReportingDate(rs.getDate("ReportingDate"));                
                arr.add(info);
            }

            stmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return arr;
    }
    
    public static ArrayList FindbyID(String ID) {
        ArrayList arr = new ArrayList();

        try {
            String QRY = "SELECT * FROM DAILYREPORT WHERE FullName LIKE(?) ORDER BY CustomerID";
            Connection con = DBManager.getInstance().getConnection();
            PreparedStatement pstmt = con.prepareStatement(QRY);
            pstmt.setString(1, "%" + ID + "%");
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                DailyReport_DTO info = new DailyReport_DTO();
                info.setReportID(rs.getString("ReportID"));
                info.setReportingDate(rs.getDate("ReportingDate"));                
                arr.add(info);
            }
            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return arr;
    }
}
